package com.javasm.common.dictionary;

import com.javasm.ProjectApplication;
import com.javasm.domin.entity.base.SysDict;
import com.javasm.domin.entity.base.SysDictType;
import com.javasm.domin.entity.orderEntity.B2cDataDictionary;
import com.javasm.mapper.B2cOrderMapper.B2cDataDictionaryMapper;
import com.javasm.mapper.redisDictMapper.SysDictMapper;
import com.javasm.mapper.redisDictMapper.SysDictTypeMapper;
import com.javasm.service.redisDictService.impl.SysDictServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Component
public class InitContext {
    /**
     * 词典全局缓存对象
     * key : type 表示哪个类型
     * value中的hashMap中的key : 表示value value表示text文本
     */
    public static final Map<Integer,LinkedHashMap<Integer,String>> dic = new HashMap<>();
    public static final Map<String,LinkedHashMap<Integer,String>> dicCache = new HashMap<>();
    //ProjectApplication是当前springboot模块的启动类，Logger类是一个日志对象 作用是在缓存好词典后，在控制台提示一下
    Logger logger = LoggerFactory.getLogger(ProjectApplication.class);
    @Resource
    private B2cDataDictionaryMapper b2cDataDictionaryMapper;
    @Resource
    SysDictServiceImpl sysDictServiceImpl;

    @Resource
    SysDictMapper sysDictMapper;

    @Resource
    SysDictTypeMapper sysDictTypeMapper;

    @Resource
    RedisTemplate<String,String> redisTemplate;

    /**
     * 容器创建自动执行
     */
    @PostConstruct
    public void init(){
        initDictionary();
        transformTypeList();
        sysDictServiceImpl.refreshCache();

    }

    /**
     * 初始化词典
     */
    public void initDictionary(){
        List<B2cDataDictionary> all = b2cDataDictionaryMapper.findAll();
        all.forEach(sysDictionary -> {
            LinkedHashMap<Integer, String> kv = dic.get(sysDictionary.getType());
            if(kv == null){
                kv = new LinkedHashMap<>();
            }
            kv.put(sysDictionary.getTypeValue(),sysDictionary.getTypeText());
            dic.put(sysDictionary.getType(),kv);
        });
        //缓存完毕，logger对象在控制台打印下面的语句进行说明，可以不要
        logger.debug("词典初始化完成");
        System.out.println("查看字典情况---->"+ dic);
        System.out.println(dic.get(1).toString());
        System.out.println(dic.get(1).get(1));
    }


    public Map<String, LinkedHashMap<Integer, String>> transformTypeList() {
        List<SysDictType> dictTypeAll = sysDictTypeMapper.getDictTypeAll();

        for (int i = 0; i < dictTypeAll.size(); i++) {
            LinkedHashMap<Integer, String> map = new LinkedHashMap<>();
            String code = dictTypeAll.get(i).getCode();
            List<SysDict> dicts = sysDictMapper.getByNameAll(code);
            for (int i1 = 0; i1 < dicts.size(); i1++) {
                map.put(dicts.get(i1).getValue(),dicts.get(i1).getName());
            }

            dicCache.put(code,map);

        }
        return dicCache;
    }


}
